#include "HAL.h"
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
#include "../macro.h"

DWORD WINAPI MyThreadFunction( LPVOID lpParam );

UInt16 HAL::timerCnt = 0;
static DWORD oldTick = 0;

void HAL::InitProject(void)
{ 
	DWORD   dwThreadIdArray;
	HANDLE  hThread;

	// Create the thread to begin execution on its own.
	hThread = CreateThread( 
		NULL,                   // default security attributes
		0,                      // use default stack size  
		MyThreadFunction,       // thread function name
		0,          // argument to thread function 
		0,                      // use default creation flags 
		&dwThreadIdArray);   // returns the thread identifier 


	// Check the return value for success.
	// If CreateThread fails, terminate execution. 
	// This will automatically clean up threads and memory. 

	if (hThread == NULL) 
	{
		ExitProcess(3);
	}


	// WaitForMultipleObjects(MAX_THREADS, hThreadArray, TRUE, INFINITE);
	// CloseHandle(hThread);
}

UInt16 HAL::ReadAccelSensor(void)
{
	return 1023u;
}

UInt16 HAL::ReadBrakeSensor(void)
{ 
	return 1023u;
}

void HAL::SetCarSpeedServo(UInt16 val)
{ 

}
void HAL::SetEngineServo(UInt16 val)
{ 

}
void HAL::SetFuelServo(UInt16 val)
{

}
void HAL::ProcessSerial()
{
	DWORD newTick= GetTickCount();
	if( (newTick - oldTick)>1000)
	{
		TRACE(Diagnostic::vSpeed);
		TRACE(" ");
		TRACE_NL(Diagnostic::vGear);

		oldTick = GetTickCount();
	}
}

DWORD WINAPI MyThreadFunction( LPVOID lpParam ) 
{ 
	while(true)
	{
		Sleep(1);
		HAL::timerCnt++;
	}
	return 0; 
}